package com.example.waterorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.waterorder.dto.UserDto;
import com.example.waterorder.entity.User;
import com.example.waterorder.mapper.UserMapper;
import com.example.waterorder.service.UserService;
import com.example.waterorder.tools.JwtHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User selectById(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public Integer register(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateById(user);
    }

    public boolean verify(UserDto userDto){
        if (StringUtils.isBlank(userDto.getToken())){
            return false;
        }
        String token = userDto.getToken();
        String exist = stringRedisTemplate.opsForValue().get("user:" + token);
        boolean flag = "exist".equals(exist);
        // 刷新token
        if (flag){
            stringRedisTemplate.opsForValue().set("user:" + token, "exist", 72L, TimeUnit.HOURS);
        }
        System.out.println(flag);
        return flag;
    }

}
